package com.aiyige.utils.widget;


import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.utils.StringUtils;
import com.aiyige.utils.UIHelper;
import com.netease.nim.uikit.common.util.string.StringUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class RequestPermissionDialog extends DialogFragment {
    Unbinder unbinder;
    String message;
    String title;
    @BindView(R.id.contentTv)
    TextView contentTv;
    @BindView(R.id.titleTv)
    TextView titleTv;
    Listener listner = null;
    @BindView(R.id.okBtn)
    TextView okBtn;

    String positive;

    public void setPositive(String positive) {
        this.positive = positive;
    }

    public String getPositive() {
        return positive;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Listener getListner() {
        return listner;
    }

    public void setListner(Listener listner) {
        this.listner = listner;
    }

    @Override
    public void onCancel(DialogInterface dialog) {
        super.onCancel(dialog);
        if (listner != null) {
            listner.onCancel();
        }
    }

    public RequestPermissionDialog() {
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public static RequestPermissionDialog newInstance(String title, String message, String positive) {
        RequestPermissionDialog fragment = new RequestPermissionDialog();
        fragment.setStyle(STYLE_NORMAL, R.style.CommonDialogTheme);
        fragment.setTitle(title);
        fragment.setMessage(message);
        fragment.setPositive(positive);
        return fragment;
    }

    public static RequestPermissionDialog newInstance(String title, String message) {
        return newInstance(title, message, StringUtils.getString(R.string.go_to_setting));
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.request_permission_dialog, container, false);
        unbinder = ButterKnife.bind(this, view);
        titleTv.setText(title);
        contentTv.setText(message);
        okBtn.setText(positive);
        contentTv.setMovementMethod(ScrollingMovementMethod.getInstance());
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        Window window = getDialog().getWindow();
        if (window != null) {
            WindowManager.LayoutParams layoutParams = window.getAttributes();
            layoutParams.width = Resources.getSystem().getDisplayMetrics().widthPixels - UIHelper.dip2px(50);
            layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setAttributes(layoutParams);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.okBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.okBtn:
                if (listner != null) {
                    listner.onClickOk();
                }
                dismiss();
                break;
        }
    }

    public interface Listener {
        void onClickOk();

        void onCancel();
    }
}
